import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { codeInspectorPlugin } from "code-inspector-plugin";

/** 图标自动引入 */
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          prefix: "i",
        }),
      ],
    }),
    Components({
      resolvers: [ElementPlusResolver(), IconsResolver({})],
    }),
    Icons({ autoInstall: true }),
    codeInspectorPlugin({
      bundler: "vite",
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    // 设置服务启动端口号
    port: 5173,
    host: "0.0.0.0",
    // 允许跨域
    cors: true,
    // 打开热更新
    hmr: true,
    // 该设置只有在开发环境下启用
    proxy: {
      "/api": {
        target: "http://0.0.0.0:4331", // 后台接口域名
        ws: true, //如果要代理 websockets，配置这个参数
        secure: false, // 如果是https接口，需要配置这个参数
        changeOrigin: true, //是否跨域
        // 请求路径重写
        // rewrite: path => path.replace(/^\/api/, ''),
      },
    },
  },
});
